package com.zjm.json;

import com.alibaba.fastjson.JSON;
import com.zjm.json.entity.ComplexObject;
import com.zjm.json.util.FastJsonUtil;
import org.junit.Test;

import java.util.Map;

/**
 * @author ZJM
 * @date 2024/4/7 23:37
 */
public class FastJsonTest02 {

    @Test
    public void test(){
        // 复杂JSON字符串
        String complexJsonString = "{ \"name\": \"John\", \"age\": 30, \"address\": { \"city\": \"New York\", \"zip\": \"10001\" }, \"phones\": [ \"123456\", \"987654\" ] }";

        // 将JSON字符串转换为对象
        ComplexObject complexObject = JSON.parseObject(complexJsonString, ComplexObject.class);
        System.out.println("Name: " + complexObject.getName());
        System.out.println("Age: " + complexObject.getAge());
        System.out.println("City: " + complexObject.getAddress().getCity());
        System.out.println("Zip: " + complexObject.getAddress().getZip());
        System.out.println("Phones: ");
        for (String phone : complexObject.getPhones()) {
            System.out.println(phone);
        }

        // 将对象转换为JSON字符串
        String jsonString = JSON.toJSONString(complexObject);
        System.out.println("JSON String: " + jsonString);
    }

    @Test
    public void test2(){
        String jsonString = "{\"name\":\"John\",\"age\":30,\"address\":{\"city\":\"New York\",\"zip\":\"10001\"},\"phones\":[\"123456\",\"987654\"]}";

        // 将JSON字符串解析为Map
        Map<String, Object> resultMap = FastJsonUtil.jsonToMap(jsonString);
        System.out.println("Result Map: " + resultMap);

        // 将Map转换为JSON字符串
        String jsonResult = FastJsonUtil.mapToJson(resultMap);
        System.out.println("JSON Result: " + jsonResult);
    }
}
